class Solution
{
public:
    bool isUnivalTree(TreeNode* root)
    {
        int ret = compare(root, root->val);
        if (ret == root->val)
            return true;
        else
            return false;
    }
    int compare(TreeNode*root, int i)
    {
        if (!root)
            return i;
        if (root->val != i)
            return i + 1;
        int left = compare(root->left, i);
        int right = compare(root->right, i);
        if (left != right)
            return i + 1;      
        return left;

        
    }
};